﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Text;
using System.Windows.Browser;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;
using Rio.Framework.Pages.AppCode.ApplicationNavigation;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Enums;
using Rio.Framework.Pages.Helpers.Utils;

namespace Rio.Framework.Pages
{
    public class Redirection
    {


        /// <summary>
        /// Run RioScan Application
        /// </summary>
        /// <param name="P_sMode"></param>
        public static void RioScan(string P_sMode)
        {
            if (GlobalWebContext.CurrentUser != null)
            {
                string path = HtmlPage.Document.DocumentUri.AbsoluteUri.Replace(
                    HtmlPage.Document.DocumentUri.LocalPath, String.Empty);
                path = path.Substring(0, path.IndexOf('#'));

                string filename = "/Rio.Application.Openscan.aspx?UID=" + GlobalWebContext.CurrentUser.RefreshScanGuid + "&MODE=" +
                                  P_sMode;

                BusinessEntitiesDomainContext l_oBecDC = new BusinessEntitiesDomainContext();
                var query = l_oBecDC.AddActionHistoricQuery((int) Constant.UserActions.NUMERISATION, 0, 0, null,
                                                            filename);
                l_oBecDC.Load(query);

                VariableDomainContext L_vdcAppVirtualPath = new VariableDomainContext();
                L_vdcAppVirtualPath.GetAppDomainAppVirtualPath(delegate(InvokeOperation<string> str)
                                                                   {
                                                                       if (str.Value == "/")
                                                                       {
                                                                           HtmlPage.Window.Navigate(
                                                                               new Uri(path + filename, UriKind.Absolute));
                                                                       }
                                                                       else
                                                                       {
                                                                           HtmlPage.Window.Navigate(
                                                                               new Uri(path + str.Value + filename,
                                                                                       UriKind.Absolute));
                                                                       }
                                                                   }
                                                               , null);
            }
        }

        /// <summary>
        /// Go to RioOffice application
        /// </summary>
        /// <param name="redirectUriAfterLogin"></param>
        public static void RioOffice(string redirectUriAfterLogin = null)
        {
            RedirectTo(ApplicationName.RioOffice, redirectUriAfterLogin);
        }

        public static void Login()
        {
            NavigationHelper.Instance.NavigateToLogin();
        }

        /// <summary>
        /// Go to RioFlow application
        /// </summary>
        public static void RioFlow()
        {
            RedirectTo(ApplicationName.RioFlow);
        }

        /// <summary>
        /// Go to RioImport application
        /// </summary>
        public static void RioImport()
        {
            RedirectTo(ApplicationName.RioImport);
        }

        /// <summary>
        /// Go to RioSearch application
        /// </summary>
        public static void RioSearch()
        {
            RedirectTo(ApplicationName.RioSearch);
        }

        /// <summary>
        /// Go to RioManager application
        /// </summary>
        public static void RioManager()
        {
            RedirectTo(ApplicationName.RioManager);
        }

        private static void RedirectTo(ApplicationName applicationName, string redirectUriAfterLogin = null)
        {
            NavigationHelper.Instance.NavigateToApplication(applicationName);
        }
    }
}
